package com.teaphy.coroutinecrawler

import kotlinx.coroutines.*


fun main() = runBlocking {
    println("runBlocking start ----> ")
    val data = getData()

    println("data: $data")

    launch(Dispatchers.IO) {
        println("launch start -->  : I'm working in thread ${Thread.currentThread().name}")

        launch(Dispatchers.IO) {
            println("launch0 start -->  : I'm working in thread ${Thread.currentThread().name}")
            delay(1000)
            println("launch0 end -->  : I'm working in thread ${Thread.currentThread().name}")
        }

        val dataLaunch = getData()
        println("launch data: $dataLaunch")

        launch(Dispatchers.IO) {
            println("launch1 start -->  : I'm working in thread ${Thread.currentThread().name}")
            delay(1000)
            println("launch1 end -->  : I'm working in thread ${Thread.currentThread().name}")
        }
        println("launch end -->  : I'm working in thread ${Thread.currentThread().name}")
    }

    println("runBlocking end ----> ")
}

suspend fun getData() : Int {
   return withContext(Dispatchers.IO) {
        delay(1000)
       println("getData : I'm working in thread ${Thread.currentThread().name}")
       1
    }
}